$(function() {
	var http = new XMLHttpRequest();
	http.open("HEAD", ".", false);
	http.send(null);
	var today = new Date(http.getResponseHeader("Date"));
	var year_today = today.getFullYear();

	var initializeItem = function(year_today,times) {
		$('#yearItem').html('');
		var selYear = year_today - times + 1;
		for(var i = 0; i < times; i++) {
			var yearTemplate = "<option value='" + selYear + "'>" + selYear + "</option>";
			$('#yearItem').append(yearTemplate);
			selYear++;
		}

		for (var i = 0; i < $("#yearItem option").length; i++) {
			if ($("#yearItem ").get(0).options[i].value == year_today) {
				$("#yearItem").get(0).options[i].selected = true;
				break;
			}
		}


	};

	initializeItem(year_today,5);
	//选择显示年份
	$("#yearItem").change(function() {
		var year = $("#yearItem option:selected").val();
		var type = $("#hideType").val() == "" ? "A" : $("#hideType").val();
		init(1,year, type);
	});


	var init = function(cpage, year, type) {
		var pageSize = 15;
		$.ajax({
			url: '/statistics/analyze',
			type: 'get',
			data: {'pageSize':pageSize,'cpage':cpage,'year':year, 'type':type, '_t': new Date().getTime()},
			success: function(data) {
				// console.info(data);
				if(data == 'noPrivilege') {
					data = {data:[],count:0};
				}else{
					if (data.count < pageSize) {
						page = 1;
					} else {
						page = Math.ceil(data.count / pageSize);
					}

					var options = {
						currentPage: cpage,
						totalPages: page,
						alignment: 'right',
						count:data.count,
						selectorId: 'pageSeletct',
						onPageChanged: function(e, oldPage, newPage) {
							init(newPage, year, type);
						}
					};

					$('#paginator').bootstrapPaginator(options);
					$("#pageSeletct").change(function() {
						var page = $("#pageSeletct option:selected").val();
						$('#paginator').bootstrapPaginator("show",page);
					});
					showList(data.data);
				}
			},
			error: function(data) {
				alert("error");
			}
		});
	};

	var key = $("#getKey").val();
	var hisPage = 1;
	var year = year_today;
	var type = 'A';
	if(key != "" && key != null && key != undefined) {
		key = JSON.parse(decodeURIComponent(key));
		hisPage = key.cpage;
		year = key.time.split("-")[0];
		type = key.type;
		if(type == 'D') {
			$("#selA").removeClass().addClass("label label-large label-grey  arrowed-right");
		    $("#selD").removeClass().addClass("label label-large label-purple  arrowed-in");
		}else {
			$("#selD").removeClass().addClass("label label-large label-grey  arrowed-in");
			$("#selA").removeClass().addClass("label label-large label-purple  arrowed-right ");
		}
		$("#hideType").val(key.type);
		for (var i = 0; i < $("#yearItem option").length; i++) {
			if ($("#yearItem ").get(0).options[i].value == year) {
				$("#yearItem").get(0).options[i].selected = true;
				break;
			}
		}
	}
	init(hisPage, year, type);

	var showList = function(data) {
		// console.info("showList",data);
		$('#anaBd').html("");
		if(data == null || data.length == 0) {
    		$('#tip').html("<div class='alert alert-warning' style='margin-top:-20px;'>未能搜索到相关信息</div>");
    	}else{
    		$('#tip').html("");
    	}
		for (var i = 0; i < data.length; i++) {
			var monthData = ["--","--","--","--","--","--","--","--","--","--","--","--"];
			for (var j = 0; j < data[i].dateList.length; j++) {
				var number = data[i].dateList[j].perfDate.substr(-1) - 1;
				monthData[number] = data[i].dateList[j].level;
			}
			

			var rData = {
				empId: data[i].rowData.employeeId,
				employeeName: data[i].rowData.name,
				monthData: monthData,
				num: data[i].rowData.num
			};
			// console.info("rData.monthData",rData.monthData);
			var modal = "<tr><input type='hidden' id='empId' value='{{empId}}'/><td class='center'>{{employeeName}}</td>"+
			"{{#monthData}}<td class='center' name='month' style='cursor:pointer;'><a href='#'>{{.}}</a></td>{{/monthData}}"+
			"<td class='center'>{{num}}</td></tr>";
			var $html = $(Mustache.to_html(modal, rData));
			$('#anaBd').append($html);

			//定义编辑按钮编辑事件
			$html.find("td[name='month']").click({
				html: $html
			}, function(e) {
				// console.info("eeee",$(e.currentTarget).find("a").html());
				// return;
				var $html = $(e.data.html);
				var month = e.currentTarget.cellIndex < 10 ? '0' + e.currentTarget.cellIndex : e.currentTarget.cellIndex
				var empId = $html.find("#empId").val();
				if($(e.currentTarget).find("a").html() != '--') {
					var time = $("#yearItem option:selected").val() + "-" + month;
					var key = {
						time: time,
						empId: empId,
						cpage: $("#pageSeletct option:selected").val(),
						type: $("#hideType").val() == "" ? "A" : $("#hideType").val()
					};
					key = encodeURIComponent(JSON.stringify(key));

					window.location.href = "/statistics/perfDetails?key=" + key;
					// alert(time);
				}

			});
		}

	};
	$("#selA").click(function() {
		$("#title").html("符合加薪员工汇总表");
		$("#selD").removeClass().addClass("label label-large label-grey  arrowed-in");
		$("#selA").removeClass().addClass("label label-large label-purple  arrowed-right ");
		var year = $("#yearItem option:selected").val();
		$("#hideType").val("A");
		init(1, year, 'A');
	}); 

	$("#selD").click(function() {
		$("#title").html("符合减薪员工汇总表");
		$("#selA").removeClass().addClass("label label-large label-grey  arrowed-right");
		$("#selD").removeClass().addClass("label label-large label-purple  arrowed-in");
		var year = $("#yearItem option:selected").val();
		$("#hideType").val("D");
		init(1, year, 'D');
	}); 

});